package file;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Loader {

	public static synchronized String load(String message) {
		// extract date from the message and load the notes for the specific
		// date. Return in one string.
		String date = message.substring(message.indexOf('2'),
				message.indexOf('2') + 10);
		String notes = readFile("notes/erik.txt", date) + "\n>---<\n"
				+ readFile("notes/johan.txt", date);
		return notes;
	}

	private static String readFile(String fileName, String date) {
		File f = new File(fileName);
		StringBuilder sb = new StringBuilder("");
		try {
			Scanner sc = new Scanner(f);
			String line = "";
			while (sc.hasNextLine()) {
				line = sc.nextLine();
				if (line.contains(date)) {
					sb.append(line);
					while (!(line = sc.nextLine()).equals("<--->")) {
						sb.append(line);
					}
					sb.append("<--->");
				}
			}
			sc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		return sb.toString();
	}
}
